package cn.ranko.shiro.service;

import cn.ranko.bean.Datagrid;
import cn.ranko.bean.Pagination;
import cn.ranko.shiro.mapper.SysMenuMapper;
import cn.ranko.shiro.mapper.SysPermissionMapper;
import cn.ranko.shiro.mapper.SysRoleMapper;
import cn.ranko.shiro.mapper.SysUserRoleMapper;
import cn.ranko.shiro.model.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * Created by user on 2017/8/29.
 */
@Service
public class SysRoleService {

    @Autowired
    SysRoleMapper sysRoleMapper;

    @Autowired
    SysUserRoleMapper sysUserRoleMapper;

    @Autowired
    SysMenuMapper sysMenuMapper;

    @Autowired
    SysPermissionMapper sysPermissionMapper;

    public Datagrid loadList(Pagination pagination) {
        Integer total = sysRoleMapper.countOfSysRole();
        List<SysUser> rows = sysRoleMapper.querySysRole(pagination);
        Datagrid datagrid = new Datagrid();
        datagrid.setTotal(total);
        datagrid.setRows(rows);
        return datagrid;
    }

    public SysRole getRole(Integer role_id) {
        SysRole role = sysRoleMapper.selectByPrimaryKey(role_id);
        this.setPermissions(role);
        return role;
    }

    public void setPermissions(SysRole role){
        List<SysPermission> permissions = sysPermissionMapper.selectByRoleId(role.getRoleId());
        StringBuffer sb_permissionstr = new StringBuffer();
        StringBuffer sb_menuidsstr = new StringBuffer();
        if(permissions.size()>0){
            for(Integer i = 0;i<permissions.size();i++){
                if(i!=0){
                    sb_permissionstr.append(",");
                    sb_permissionstr.append(permissions.get(i).getPermission());

                    sb_menuidsstr.append(",");
                    sb_menuidsstr.append(permissions.get(i).getMenuId());
                }else{
                    sb_permissionstr.append(permissions.get(i).getPermission());

                    sb_menuidsstr.append(permissions.get(i).getMenuId());
                }
            }
            role.setPermissionstrs(sb_permissionstr.toString());
            role.setMenuIdstrs(sb_menuidsstr.toString());
        }
    }

    private String getPermissionstrs(Integer roleId) {
        List<SysPermission> permissions = sysPermissionMapper.selectByRoleId(roleId);
        StringBuffer stringBuffer = new StringBuffer();
        if(permissions.size()>0){
            for(Integer i = 0;i<permissions.size();i++){
                if(i==permissions.size()){
                    stringBuffer.append(",");
                    stringBuffer.append(permissions.get(i).getPermission());
                }else{
                    stringBuffer.append(permissions.get(i).getPermission());
                }
            }
        }
        return stringBuffer.toString();
    }

    /**
     * 根据Uuser_id，获取用户和权限的对应的关系的列表
     * @param user_id
     * @return
     */
    public List<SysUserRole> getSysUserRoleByUserId(Integer user_id) {
        return sysUserRoleMapper.getSysUserRoleByUserId(user_id);
    }

    public List<SysRole> loadAll() {
        return sysRoleMapper.loadAll();
    }

    public Integer deleteRole(Integer roleId) {
        SysRole role = new SysRole();
        role.setRoleId(roleId);
        role.setDeleted(1);
        return sysRoleMapper.updateByPrimaryKeySelective(role);
    }

    public Integer insertRole(SysRole role) {
        return sysRoleMapper.insertSelective(role);
    }

    public Integer updateRole(SysRole role) {
        return sysRoleMapper.updateByPrimaryKeySelective(role);
    }
}
